#include "../main.h"

char *getlocaltime()
{
    struct tm *tp;
    time_t t = time(NULL);
    tp = localtime(&t);
    char *hour = malloc(sizeof(int));
    char *min = malloc(sizeof(int));
    char *sec = malloc(sizeof(int));
    sprintf(hour, "%d:", tp->tm_hour);
    sprintf(min, "%d:", tp->tm_min);
    sprintf(sec, "%d", tp->tm_sec);
    char *times = charJoin(hour, min);
    times = charJoin(times, sec);
    return times;
}
char *getlocaldate()
{
    struct tm *tp;
    time_t t = time(NULL);
    tp = localtime(&t);
    char *year = malloc(sizeof(int));
    char *month = malloc(sizeof(int));
    char *day = malloc(sizeof(int));
    sprintf(year, "%d\\", tp->tm_year + 1900);
    sprintf(month, "%d\\", tp->tm_mon);
    sprintf(day, "%d", tp->tm_mday);
    char *date = charJoin(year, month);
    date = charJoin(date, day);
    return date;
}

char *getlocaltimewithdate()
{
    struct tm *tp;
    time_t t = time(NULL);
    tp = localtime(&t);
    char *hour = malloc(sizeof(int));
    char *min = malloc(sizeof(int));
    char *sec = malloc(sizeof(int));
    sprintf(hour, "%d:", tp->tm_hour);
    sprintf(min, "%d:", tp->tm_min);
    sprintf(sec, "%d", tp->tm_sec);
    char *times = charJoin(hour, min);
    times = charJoin(times, sec);

    char *year = malloc(sizeof(int));
    char *month = malloc(sizeof(int));
    char *day = malloc(sizeof(int));
    sprintf(year, "%d/", tp->tm_year + 1900);
    sprintf(month, "%d/", tp->tm_mon);
    sprintf(day, "%d", tp->tm_mday);
    char *date = charJoin(year, month);
    date = charJoin(date, day);
    date = charJoin(date, " ");
    return charJoin(date, times);
}

int charToInt(char *data)
{
    unsigned int len = (unsigned char)*(data + 0) << 24 | (unsigned char)*(data + 1) << 16 | (unsigned char)*(data + 2) << 8 | (unsigned char)*(data + 3);
    return len;
}

int get_size(char *data)
{
    char msg_length_buffer[4];
    bzero(msg_length_buffer, 4);
    memcpy(msg_length_buffer, data, sizeof(int));
    int msg_size = charToInt(msg_length_buffer);
    return msg_size;
}